package com.concurrency.example.singleton;

import com.concurrency.annoations.ThreadSafe;

/**
 * 懒汉模式
 * volatile +  双重检查锁
 */
@ThreadSafe
public class SingletonExample2 {

    private static volatile SingletonExample2 instance = null;

    private SingletonExample2(){}

    // 对象初始化在内存中的步骤
    //1、memory = allocate()  分配对象的内存空间
    //2、ctorInstanche()      初始化对象
    //3、instance = memory    设置instance指向刚分配的内存


    public static synchronized SingletonExample2 getInstance(){
        if(instance == null){         //这里可能有问题
            synchronized (SingletonExample2.class){
                if(instance == null){
                    instance = new SingletonExample2();
                }
            }
        }
        return instance;
    }

}
